<?php
/**
 * Created by PhpStorm.
 * User: Earl
 * Date: 2016/11/4
 * Time: 16:21
 * Modify :YWH
 * Date:2017-03-06
 */
namespace Home\Controller;
use Api\Controller\BaseApiController;
use Push\Controller\ApiController;
use Push\Controller\PushController;




class NotifyController extends BaseApiController
{

//    The Notfiy from ALI
     public function  aliPay(){

          vendor('Alipay',VENDOR_PATH.'Alipay','.class.php');

         $alipay = new \Alipay\Alipay();

         $res = $alipay->verifyNotify();

         file_put_contents('./Uploads/res.txt',json_encode($res));

         file_put_contents('./Uploads/post.txt',json_encode($_POST));

         $order_no=$res['out_trade_no'];

         $order   =  M('recharge_order');

//         Query order exists
         $result = $order->where(array('order_no'=>$order_no,'status'=>0))->find();

         file_put_contents('./Uploads/$result.txt',json_encode($result));

         file_put_contents('./Uploads/sql1.txt',$order->_sql());

//         if exists  order  Then  the code is executed
         if ($result) {

             $score     = $result['score'];

//             modify order status
             $data = array(
                 'status'                       => '1',
                 'pay_channel'             => 'alipay',
                 'update_time'             => NOW_TIME
             );

             $flag = $order->where(array('order_no'=>$order_no))->setField($data);

             file_put_contents('./Uploads/sql2.txt',$order->_sql());

//             Modify the order status, modify the balance of the corresponding user, to achieve the user's increase
             if ($flag !==false){
                $r =  M('user')->where(array('uid'=>$result['uid']))->setInc('score',$score);

                 file_put_contents('./Uploads/r.txt',$r);

                 file_put_contents('./Uploads/sql3.txt', M('user')->_sql());

                 $push = new ApiController();
                 $content=array('title'=>'充值提醒','list'=>array(),'type'=>'4','score'=>$score);
                 $a = $push->sendMsgToUser($result['uid'],$content,1);
                 file_put_contents('./Uploads/a.txt',$a);

                 echo 'success';
             }
         }
     }


//    backgroundNotify form Wechat
    public function  wxPay(){

        file_put_contents('./Uploads/res.txt',json_encode($res));

        file_put_contents('./Uploads/post.txt',json_encode($_POST));

        $order_no=$_POST['out_trade_no'];

        $order   =  M('recharge_order');

//         Query order exists
        $result = $order->where(array('order_no'=>$order_no,'status'=>0))->find();

        file_put_contents('./Uploads/$result.txt',json_encode($result));

        file_put_contents('./Uploads/sql1.txt',$order->_sql());

//         if exists  order  Then  the code is executed
        if ($result) {

            $score     = $result['score'];

//             modify order status
            $data = array(
                'status'                       => '1',
                'pay_channel'             => 'wxpay',
                'update_time'             => NOW_TIME
            );

            $flag = $order->where(array('order_no'=>$order_no))->setField($data);

            file_put_contents('./Uploads/sql2.txt',$order->_sql());

//             Modify the order status, modify the balance of the corresponding user, to achieve the user's increase
            if ($flag !==false){
                $r =  M('user')->where(array('uid'=>$result['uid']))->setInc('score',$score);

                file_put_contents('./Uploads/r.txt',$r);

                file_put_contents('./Uploads/sql3.txt', M('user')->_sql());

                $push = new ApiController();
                $content=array('title'=>'充值提醒','list'=>array(),'type'=>'4','score'=>$score);
                $a = $push->sendMsgToUser($result['uid'],$content,1);
                file_put_contents('./Uploads/a.txt',$a);

                echo 'success';
            }
        }



    }



}